package com.webserver.core;

import com.webserver.annotations.Controller;
import com.webserver.annotations.RequestMapping;

import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 当前类用于维护所有请求与对应的Controller中处理该请求的方法
 */
public class HandlerMapping {
    /**
     *  key:请求路径
     *  value:处理该请求的Controller的对应方法
     */
    private static Map<String,MethodMapping> mapping = new HashMap<>();

    static{
        initMapping();
    }

    private static void initMapping(){
        try{
            File dir = new File(
                    WebServerApplication.TARGET.getResource("./controller").toURI()
            );
            File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
            for (File sub : subs) {
                //根据文件名截取类名并加载
                String fileName = sub.getName();
                String className = fileName.substring(0, fileName.indexOf("."));
                //根据启动WebServer的靶点类获取它的包名,因为controller包也在该包中
                String packageName = WebServerApplication.TARGET.getPackage().getName();

                Class cls = Class.forName(packageName+".controller." + className);
                //判断当前类是否被@Controller标注
                if(cls.isAnnotationPresent(Controller.class)){
                    Object obj = cls.newInstance();//实例化该Controller
                    //获取该类定义的所有方法
                    Method[] methods = cls.getDeclaredMethods();
                    for(Method method : methods){
                        //判断该方法是否被注解@RequestMapping标注过
                        if(method.isAnnotationPresent(RequestMapping.class)){
                            //获取该注解并取得注解参数来判断本次请求是否应当被该方法处理
                            RequestMapping rm = method.getAnnotation(RequestMapping.class);
                            String value = rm.value();
                            //将方法对象以及该方法的所属对象(该Controller实例obj)以一个MethodMapping实例表示
                            MethodMapping methodMapping = new MethodMapping(method,obj);
                            mapping.put(value,methodMapping);
                        }
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 根据请求路径获取处理该请求的Controller实例以及对应的方法对象
     * @param path
     * @return
     */
    public static MethodMapping getMethod(String path){
        return mapping.get(path);
    }

    public static void main(String[] args) {
        System.out.println(mapping.size());
    }

    public static class MethodMapping{
        private Method method;      //处理某个请求的方法
        private Object controller;  //该方法所处的Controller实例

        public MethodMapping(Method method, Object controller) {
            this.method = method;
            this.controller = controller;
        }

        public Method getMethod() {
            return method;
        }

        public void setMethod(Method method) {
            this.method = method;
        }

        public Object getController() {
            return controller;
        }

        public void setController(Object controller) {
            this.controller = controller;
        }
    }
}
